package com.ld.collegetask;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.google.gson.Gson;
import com.ld.collegetask.beans.BaseJsonBean;
import com.ld.collegetask.beans.MemberCashBean;
import com.ld.collegetask.beans.MemberCashListBean;
import com.ld.collegetask.databinding.ActivityMemberCashListBinding;
import com.ld.collegetask.serverInterface.RequestSerives;
import com.ld.collegetask.utils.AppApplication;
import com.ld.collegetask.utils.SharePreference;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class MemberCashListActivity extends AppCompatActivity {

    private ActivityMemberCashListBinding binding;

    private Integer userId = 0;
    private Integer pageNo = 0;
    private Integer totalPage = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_member_cash_list);

        SharePreference sp = new SharePreference(getBaseContext());
        userId = sp.getUserId();

        member_cash_list();
    }

    private void member_cash_list() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(AppApplication.server).addConverterFactory(ScalarsConverterFactory.create()).build();
        RequestSerives requestSerives = retrofit.create(RequestSerives.class);
        Call<String> call = requestSerives.member_cash_list(userId, pageNo);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Gson gson = new Gson();
                BaseJsonBean base = gson.fromJson(response.body().toString(), BaseJsonBean.class);
                if ("1".equals(base.getCode())) {
                    MemberCashListBean memberCashList = gson.fromJson(base.getJson(), MemberCashListBean.class);
                    pageNo = memberCashList.getPageNo();
                    totalPage = memberCashList.getTotalPage();
                    ContentList.addAll(memberCashList.getCashList());
                    setListAdapter(ContentList);
                } else {

                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });
    }

    private MemberCashListBean memberCashListBean;
    private List<MemberCashBean> ContentList = new ArrayList<>();
    private MemberCashListActivity.MyListAdapter<MemberCashBean> adapter;

    private void setListAdapter(List<MemberCashBean> list) {
        if (pageNo <= 1) {
            adapter = new MemberCashListActivity.MyListAdapter(this, list, R.layout.item_member_cash_list, com.ld.collegetask.BR.memberCashBean);
            binding.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }

    public class MyListAdapter<T> extends BaseAdapter {
        private Context context;//上下文环境
        private List<T> list;//通用的，不知道数据
        private int layoutId;//通用的，不知道布局
        private int variableId;//变量的id

        // 通过构造器关联数据源与数据适配器
        public MyListAdapter(Context context, List<T> list, int layoutId, int variableId) {
            this.context = context;
            this.list = list;
            this.layoutId = layoutId;
            this.variableId = variableId;
        }

        @Override
        public int getCount() {
            if (list != null)
                return list.size();
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (position == list.size() - 1) {
                if (pageNo < totalPage) {
                    pageNo++;
                    member_cash_list();
                }
            }

            ViewDataBinding viewDataBinding = null;
            if (convertView == null) {
                viewDataBinding = DataBindingUtil.inflate(LayoutInflater.from(context), layoutId, parent, false);
            } else {
                viewDataBinding = DataBindingUtil.getBinding(convertView);
            }
            viewDataBinding.setVariable(variableId, list.get(position));
            return viewDataBinding.getRoot();
        }
    }
}
